<template>
  <el-row class="tree" type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
    <el-col>
      <strong v-if="isCompany">{{ nodeData.name }}</strong>
      <span v-else>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDeptsFn">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isCompany" @click.native="editDeptsFn">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isCompany" @click.native="delDeptsFn">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    // 部门列表数据
    nodeData: {
      type: Object,
      required: true
    },
    // 是否为公司的数据
    isCompany: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 当点击添加子部门是弹框
    addDeptsFn() {
      // 弹框,通知父组件将弹框值为true
      // 点击新增子部门时，还需要获取到对应对象的id
      this.$emit('visibleFn', this.nodeData)
    },
    // 删除部门
    async delDeptsFn() {
    // 二次确认
      await this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消' })
      // 发送请求
      await delDepartment(this.nodeData.id)
      // 提示
      this.$message.success('删除成功')
      // 刷新页面
      this.$emit('reloadPage')
    },
    // 编辑部门
    editDeptsFn() {
      // 通知父组件弹框
      this.$emit('editVisible', this.nodeData)
    }
  }
}
</script>

<style>

</style>
